package com.unswift.window.timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.Timer;

import com.unswift.QuickApplication;
import com.unswift.utils.DateUtils;
import com.unswift.utils.ObjectUtils;
import com.unswift.utils.ThreadUtils;

public class ExeTimeTimer {

	private static Timer timer;
	private static long startTime;
	
	public synchronized static void start(final long startTime){
		if(ObjectUtils.isNotEmpty(timer)){
			timer.stop();
			try {
				Thread.sleep(50);
			} catch (Exception e) {
				e.printStackTrace();
			}
			timer=null;
		}
		ExeTimeTimer.startTime=startTime;
		ThreadUtils.execute(new Runnable() {
			@Override
			public void run() {
				timer=new Timer(50, new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						BigDecimal exeSeconds = DateUtils.millisToSecond(System.currentTimeMillis()-startTime, 3);
						QuickApplication.setExeTime(exeSeconds+"秒");
					}
				});
				timer.start();
			}
		});
	}
	
	public synchronized static void stop(){
		if(ObjectUtils.isNotEmpty(timer)){
			timer.stop();
			BigDecimal exeSeconds = DateUtils.millisToSecond(System.currentTimeMillis()-startTime, 3);
			QuickApplication.setExeTime(exeSeconds+"秒");
			timer=null;
		}
	}
}
